package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.List;

public class EditPage extends Utils {
    public static String url = "http://8.137.19.140:9090/blog_edit.html";

    public EditPage() {
        super(url);
    }

    /**
     * 正常发布博客
     */
    public void editSuc() throws InterruptedException {
        //创建的博客标题要随机生成，具备唯一性，方便后面进行精确校验
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String titleTime = sim.format(System.currentTimeMillis());
        String title = "自动创建博客-"+titleTime;

        //找到标题输入框并输入内容
        driver.findElement(By.cssSelector("#title")).sendKeys("Java115-autotest标题");
//        //找到内容区
//        driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")).sendKeys("Java115&Java116-autotest内容");

        //解决方法二：通过原生的鼠标键盘操作来处理该元素-Actions
        WebElement ele = driver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre"));
        Actions action = new Actions(driver);
        action.doubleClick(ele).sendKeys("内容内容内容内容").perform();
        action.click(ele).sendKeys("内容内容内容内容").perform();

        Thread.sleep(3000);
        //点击发布
        driver.findElement(By.cssSelector("#submit")).click();


        //校验是否发布成功
        //发布成功之后跳转到列表页，找是否存在刚创建的博客————博客标题
        List<WebElement> blogs = driver.findElements(By.cssSelector("body > div.container > div.right > div"));

        //body > div.container > div.right > div:nth-child(5) > div.title
        String titleAfter = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child("+blogs.size()+") > div.title")).getText();

        assert titleAfter.equals(title);

    }

    /**
     * 异常发布————下去自行实现
     * ————标题为空
     * ————内容为空
     * ————标题内容都为空
     */
}







































